<?php


namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class EditorUploadController extends Controller
{
    public function upload(Request $request)
    {
        $request->validate([
            'file' => 'required|image|max:2048',
        ]);

        $file = $request->file('file');

        $path = '/assets/img/cms/new_cms_img/images/' . date('Y/m/d');

        $fileName = Str::random(16) . '.' . $file->getClientOriginalExtension();

        $disk = 'remote_storage';
        $result = Storage::disk($disk)->putFileAs($path, $file, $fileName);

        if (!$result) {
            return response()->json([
                'success' => false,
                'message' => '文件上传失败'
            ]);
        }

        $url = '/storage/'.$result;

        return response()->json([
            'success' => true,
            'location' => $url
        ]);
    }
}
